package com.niit.house.tgq.type;


import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;






public class TypeReducer extends Reducer<Text, Text, Text, Text> {





    @Override
    protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        int[] houses = {0, 0, 0, 0};

        double[] rates = {0, 0, 0, 0};

        int totalHouses = 0;

        for (Text val : values) {
            totalHouses++;
            if ((val.toString()).equals("塔楼")) {
                houses[0]++;
            } else if ((val.toString()).equals("平房")) {
                houses[1]++;
            } else if ((val.toString()).equals("板塔结合")) {
                houses[2]++;
            } else if ((val.toString()).equals("板楼")) {
                houses[3]++;
            }
        }

        for (int i = 0; i < 4; i++){
            rates[i] = (double) houses[i] / totalHouses;
            rates[i] *= 100;
        }


        context.write(new Text(String.format("%s", key.toString())), new Text(String.format("塔楼: %.2f%%, 平房: %.2f%%, 板塔结合: %.2f%%, 板楼: %.2f%%", rates[0], rates[1], rates[2], rates[3])));

    }




}
